package sddz.foodmanage.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import sddz.foodmanage.R;
import sddz.foodmanage.base.BaseDialog;

/**
 * Created by zhanglei on 2017/4/7 0007.
 * 打电话的popupwindow
 */

public class CallWindow extends BaseDialog implements View.OnClickListener {

    private Activity mActivity;
    private PopupWindow popupWindow;
    private String phoneNumber = "15075896321";
    private TextView send;
    private TextView phone_number;
    private TextView cancel;
    private View view;

    public CallWindow(Activity mActivity) {
        super(mActivity);
        this.mActivity = mActivity;
        initView();
        initListener();
    }

    private void initListener() {
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1f);
            }
        });

        cancel.setOnClickListener(this);
        send.setOnClickListener(this);
    }

    private void initView() {
        view = LayoutInflater.from(mActivity).inflate(R.layout.call_layout, null);
        send = (TextView) view.findViewById(R.id.call_layout_send);
        phone_number = (TextView) view.findViewById(R.id.call_layout_phone_number);
        cancel = (TextView) view.findViewById(R.id.call_layout_cancel);

        phone_number.setText(phoneNumber);

        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.take_photo_anim);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    /**
     * 显示dialog
     */
    public void showDiaLog() {
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        setAlpha(0.6f);
    }

    /**
     * 隐藏dialog
     */
    public void hideDiaLog() {
        popupWindow.dismiss();
    }

    /**
     * 设置手机号
     */
    public void setPhoneNumber(String number) {
        if (!TextUtils.isEmpty(number)) {
            phoneNumber = number;
            phone_number.setText(number);
        } else {
            phone_number.setText("暂无手机号");
            phoneNumber = "";
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //取消
            case R.id.call_layout_cancel:
                hideDiaLog();
                break;
            //拨号
            case R.id.call_layout_send:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                if (!TextUtils.isEmpty(phoneNumber)) {
                    Uri data = Uri.parse("tel:" + phoneNumber);
                    intent.setData(data);
                    mActivity.startActivity(intent);
                }
                break;
        }
    }
}
